package com.springboot.example.mybatis;

import com.springboot.example.monitor.interceptor.HandlerInterceptor;
import com.springboot.example.monitor.interceptor.UserTestInterceptor;
import com.springboot.example.mybatis.application.WebConfig;
import com.springboot.example.mybatis.domain.User;
import com.springboot.example.mybatis.mapper.UserMapper;
import com.springboot.example.mybatis.servlet.MessageServlet;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import javax.annotation.Resource;

/**
 * 作者: guo yao
 * 日期: 2016年01月14日
 * 时间: 下午14:45
 */
//@EnableScheduling
//@EnableWebMvc
@SpringBootApplication
@ServletComponentScan  //servlet注解
@MapperScan("com.springboot.example.monitor.mapper")
public class SpringBootServer extends WebConfig implements CommandLineRunner{
//public class SpringBootServer extends SpringBootServletInitializer implements CommandLineRunner {

    @Resource
    private UserMapper userMapper;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringBootServer.class);
    }


    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootServer.class);
        app.run(args);
    }

    /**
     * 启动执行的代码
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        System.out.println(userMapper.select(new User()));
    }

    @Bean
    public com.springboot.example.mybatis.interceptor.DialectStatementHandlerInterceptor dialectStatementHandlerInterceptor() {
        System.out.println("=====================dialectStatementHandlerInterceptor==========");
        return new com.springboot.example.mybatis.interceptor.DialectStatementHandlerInterceptor();
    }

    @Bean
    public com.springboot.example.mybatis.interceptor.DialectResultSetHandlerInterceptor dialectResultSetHandlerInterceptor() {
        System.out.println("=====================dialectResultSetHandlerInterceptor==========");
        return new com.springboot.example.mybatis.interceptor.DialectResultSetHandlerInterceptor();
    }



    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("classpath:/spring-mvc-config.xml");
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserTestInterceptor()).addPathPatterns("/users*//**//**");
        registry.addInterceptor(new HandlerInterceptor()).addPathPatterns("/users*//**//**");
    }

    // 定义一个servlet
    /*@Bean
    public ServletRegistrationBean MessageMessageServlet() {
        return new ServletRegistrationBean(new MessageServlet(), "/servlet*//*");
    }*/
}
